package com.example.camerademoexcercies1.tool;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.media.Image;
import android.os.Environment;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;

import static com.example.camerademoexcercies1.tool.BitmapTool.rotateBitmapByDegree;

public class ImageTool {

    public static String saveImage(Image image,String cameraId,String rootPath) {
        String finalPath = rootPath + System.currentTimeMillis() + ".jpg";
        new ImageSaver(image, cameraId, finalPath).run();
        return finalPath;
    }

    //    创建保存图片的线程
    public static class ImageSaver implements Runnable {
        private Image mImage;
        private String cameraId;
        private String finalPath;

        public ImageSaver(Image mImage, String cameraId, String finalPath) {
            this.mImage = mImage;
            this.cameraId = cameraId;
            this.finalPath = finalPath;
        }

        @Override
        public void run() {
//        getPlanes()返回一个Plane[]矩阵数组 拿取第一个Plane使用getBuffer()获得该像素矩阵的ByteBuffer--字节缓存
            ByteBuffer buffer = mImage.getPlanes()[0].getBuffer();
//        返回该缓冲区最大数据长度
            byte[] data = new byte[buffer.remaining()];
            buffer.get(data);

            File finalFile = null; //最终文件路径
            //路径尾部
            String tailName = System.currentTimeMillis() + ".jpg";
            //DCIM的根目录 /storage/emulated/0/DCIM
            File root = new File(String.valueOf(Environment.getExternalStoragePublicDirectory(
                    Environment.DIRECTORY_DCIM)), "CameraTestStorage");
            //拿到根目录开始操作输出流
            FileOutputStream fos = null;
            Bitmap bitmap = rotateBitmapByDegree(BitmapFactory.decodeByteArray(data, 0, data.length), (cameraId.equals("0") ? 90 : 270));
            new savePreviewBitmapThread(bitmap, finalPath).run();
//            if (root.isDirectory() || root.mkdir()) {
//                finalFile = new File(root, tailName); //absolute路径
//                try {
//                    fos = new FileOutputStream(finalFile);
//                    fos.write(data, 0, data.length);
//                    LogTool.log(finalFile.toString() + "保存成功");
////                    通知系统图库更新
//                    notifyUpdate(finalFile);
//                } catch (Exception e) {
//                    e.printStackTrace();
//                } finally {
////                    关闭输出流 清空图像file缓存
//                    finalFile = null;
//                    if (fos != null) {
//                        try {
//                            fos.close();
//                            fos = null;
//                            captureImage.close();
//                            takePhotoButton.setClickable(true);
//                        } catch (IOException e) {
//                            e.printStackTrace();
//                        }
//                    }
//                }
//            } else {
//                LogTool.log("根目录+" + root.toString() + "+创建失败");
//            }
        }

        //    Bitmap保存线程
        private static class savePreviewBitmapThread implements Runnable {

            private Bitmap bitmap;
            private String finalPath;

            public savePreviewBitmapThread(Bitmap bitmap, String finalPath) {
                this.bitmap = bitmap;
                this.finalPath = finalPath;
            }

            @Override
            public void run() {
                savePreviewBitmap(bitmap);
            }

            //    保存bitmap到本地
            private void savePreviewBitmap(Bitmap bitmap) {
                File finalImage = new File(finalPath);
                try {
                    FileOutputStream fos = new FileOutputStream(finalImage);
                    bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);
                    fos.flush();
                    fos.close();
                    say("保存图片到： " + finalImage.toString());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }


    //    打印
    public static void say(String word) {
        LogTool.log(word);
    }
}
